using System;
using System.Windows.Forms;

using FridayShopping.DataModel;

namespace FridayShopping.UI.ListFormManagers
{
	/// <summary>
	/// Summary description for StatsListFormManager.
	/// </summary>
	public class StatsListFormManager : ListFormManager
	{
		public StatsListFormManager(IListViewForm form, ListView listView, IItemList list) : 
			   base(form, listView, list)
		{
		}

		public override void EnableDisableItems()
		{
			bool toEnable = false;
			if (listView.SelectedIndices.Count > 0) 
			{
				toEnable = listView.FocusedItem.Selected;
			}
			((StatisticsForm)form).toolBarButton_Usual.Enabled = toEnable;
		}

		protected override ListViewItem CreateListViewItem(FridayShopping.DataModel.ItemBase item)
		{
			StatisticsInfo i = (StatisticsInfo)item;
			int avg = i.TotalAmount/i.Count;
			Product product = (Product)Database.Instance.Products.GetItem(i.Key);
			string name = "";
			if (product != null) 
			{
				name = product.Name;
			} 
			string[] subitems = {
									i.Key.ToString(),
									name,
									i.Count.ToString(),
									avg.ToString()
								};
			return new ListViewItem(subitems);
		}

		public override void ToolbarEventsHandler(ToolBarButtonClickEventArgs e)
		{
			if (e.Button == ((StatisticsForm)form).toolBarButton_Usual) 
			{
				// Ask if we can
				string text = LangResources.Instance.Resources.GetString("setusual");
				string caption = LangResources.Instance.Resources.GetString("applName");
				if (MessageBox.Show(text,caption,MessageBoxButtons.YesNo,
									MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) == DialogResult.Yes)
				{
					Key key = GetItemKey(listView.FocusedItem);
					Product product = (Product)Database.Instance.Products.GetItem(key);
					if (product != null)
					{
						product.Usual = GetItemAmount(listView.FocusedItem);
					}
				}
			}
			if (e.Button == form.DeleteButton)
			{
				if (MessageBox.Show(LangResources.Instance.Resources.GetString("resetStats"),
					LangResources.Instance.Resources.GetString("applName"),
					MessageBoxButtons.YesNo,
					MessageBoxIcon.Question,
					MessageBoxDefaultButton.Button2) == DialogResult.Yes) 
				{

					listView.Items.Clear();
					Statistics.Instance.ResetStatistics();                    
				}
				return;
			}
		}

		private int GetItemAmount(ListViewItem item)
		{
			int amount = Convert.ToInt32(item.SubItems[3].Text);
			return amount;
		}            


	}
}
